package com.tg.app.activity.device.add;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.appbase.custom.config.ApiUrl;
import com.tg.app.R;
import com.tg.app.activity.device.DeviceSettingsActivity;
import com.tg.app.helper.TGGlobalConfigHelper;
import com.tange.base.toolkit.StringUtils;
import com.tg.data.bean.DeviceSettingsInfo;
import com.tg.app.helper.ActivityHelper;
import com.tg.app.listener.OnAddDeviceListener;
import com.tg.app.util.ObjectBoxUtil;
import com.tg.app.util.UriUtil;
import com.tange.base.toolkit.DimenUtil;
import com.tg.appcommon.android.TGApplicationBase;
import com.tg.appcommon.android.TGLog;
import com.tg.appcommon.android.TGToast;
import com.tg.data.bean.DeviceSettingsInfo_;

import io.objectbox.Box;

public class Reset4gCarGuideNewFragment extends Fragment {
    public static final String TAG = Reset4gCarGuideNewFragment.class.getSimpleName();
    private static final int HEIGHT = 700;
    private OnAddDeviceListener mOnAddDeviceListener;
    private CheckBox mCbListener;
    private int mParam;
    private String mUUID;
    public Reset4gCarGuideNewFragment() {
    }

    public static Reset4gCarGuideNewFragment newInstance(String uuid) {
        Reset4gCarGuideNewFragment fragment = new Reset4gCarGuideNewFragment();
        Bundle args = new Bundle();
        args.putString(DeviceProgressFragment.ARG_PARAM_UUID, uuid);
        fragment.setArguments(args);
        return fragment;
    }
    public void setOnAddDeviceListener(OnAddDeviceListener onAddDeviceListener) {
        this.mOnAddDeviceListener = onAddDeviceListener;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam = getArguments().getInt(AddFragmentTabActivity.EXT_DEVICE_ADD_TYPE);
            mUUID = getArguments().getString(DeviceProgressFragment.ARG_PARAM_UUID);
        }
        setToolbar();
    }

    @Override
    public void onResume() {
        super.onResume();
        setToolbar();
    }


    private void setToolbar() {
        ScanQrcodeActivity addFragmentTabActivity = (ScanQrcodeActivity) getActivity();
        if (addFragmentTabActivity != null) {
            addFragmentTabActivity.showRightOfToolbar(false);
            if (addFragmentTabActivity.getBackButton() != null) {
                addFragmentTabActivity.getBackButton().setVisibility(View.VISIBLE);
                addFragmentTabActivity.getBackButton().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ActivityHelper.gotoDeviceListPage(getActivity());
                    }
                });
            }

        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getArguments() != null) {
            mParam = getArguments().getInt(AddFragmentTabActivity.EXT_DEVICE_ADD_TYPE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View  view =  inflater.inflate(R.layout.fragment_reset_4g_car_guide_new, container, false);
        TextView textTip = view.findViewById(R.id.tv_device_wifi_add_title);
        if (mParam == AddFragmentTabActivity.DEVICE_ADD_TYPE_QRCODE_SCAN){
            textTip.setText(R.string.wifi_4g_device_add);
        }
        mCbListener = view.findViewById(R.id.cb_listener);
        final Button button = view.findViewById(R.id.btn_before_add_next);
        button.setAlpha(0.4f);
        button.setOnClickListener(view1 -> {
            if (!mCbListener.isChecked()){
                TGToast.showToast(R.string.toolbar_scan_device_submit);
            }else{
                if (mOnAddDeviceListener !=null){
                    mOnAddDeviceListener.onClickTimeout();
                }
            }
        });

        view.findViewById(R.id.app_icon3).setOnClickListener(v -> {
            Box<DeviceSettingsInfo> deviceSettingsInfoBox = ObjectBoxUtil.getDeviceSettingsInfo();
            DeviceSettingsInfo info = deviceSettingsInfoBox.query().equal(DeviceSettingsInfo_.uuid, mUUID).build().findFirst();
            Intent intent = new Intent();
            intent.putExtra(DeviceSettingsActivity.EXT_DEVICE_INFO, info);
            String url = TGGlobalConfigHelper.getInstance().getHowToResetDevice();
            if (StringUtils.isEmpty(url)){
                url = ApiUrl.APP_HELP_RESET;
            }
            UriUtil.openServePlayImpl(getActivity(), intent, url, "", 0);
        });

        mCbListener.setOnCheckedChangeListener((compoundButton, b) -> button.setAlpha(b? 1f : 0.4f));

        int h = getResources().getDisplayMetrics().heightPixels;
        float height = DimenUtil.px2dp(TGApplicationBase.getApplicationContext(), h);
        TGLog.d(TAG + "height = " + height);
        if (height < HEIGHT){
           TextView textView = view.findViewById(R.id.tv_device_ap_add_title);
           textView.setTextSize(18f);
           textTip.setTextSize(14f);
           RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)button.getLayoutParams();
           layoutParams.bottomMargin = DimenUtil.dp2px(TGApplicationBase.getApplicationContext(), 15);
           button.setLayoutParams(layoutParams);
        }

        return view;
    }
}
